package com.lottery.material.controller.admin.portaluser;

import com.lottery.material.common.EnumInfo;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.enumeration.portaluser.PortalUserInfoEnum;
import com.lottery.material.enumeration.transaction.UserBuyRecordEnum;
import com.lottery.material.pojo.conf.AreaDict;
import com.lottery.material.pojo.conf.BankDict;
import com.lottery.material.pojo.conf.UserLevelDict;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojo.portaluser.PortalUserInfo;
import com.lottery.material.pojocustom.portaluser.PortalUserInfoCustom;
import com.lottery.material.service.admin.conf.AreaDictService;
import com.lottery.material.service.admin.conf.BankDictService;
import com.lottery.material.service.admin.conf.UserLevelDictService;
import com.lottery.material.service.admin.portaluser.PortalUserInfoService;
import com.lottery.material.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * Description：前端用户信息Controller
 * Created by 朱正磊 on 2019-04-13 20:20
 */
@Slf4j
@Controller("adminPortalUserInfoController")
@RequestMapping("/admin/portalUserInfo")
public class PortalUserInfoController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/portalUserInfo/list";

    private final PortalUserInfoService portalUserInfoService;

    private final BankDictService bankDictService;

    private final AreaDictService areaDictService;

    private final UserLevelDictService userLevelDictService;

    @Autowired
    public PortalUserInfoController(PortalUserInfoService portalUserInfoService,
                                    BankDictService bankDictService,
                                    AreaDictService areaDictService,
                                    UserLevelDictService userLevelDictService) {
        this.portalUserInfoService = portalUserInfoService;
        this.bankDictService = bankDictService;
        this.areaDictService = areaDictService;
        this.userLevelDictService = userLevelDictService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        //将pd放到session里面以备后用（导出Excel）
        setAttributeToSession("portalUserInfoPd", pd);
        page.setPd(pd);

        //查询分页列表
        List<PortalUserInfoCustom> list = portalUserInfoService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        //用户状态（1：启用；2：禁用；3：已删除）
        List<EnumInfo> userStatusList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.USER_STATUS, PortalUserInfoEnum.class);
        map.put("userStatusList", userStatusList);

        //用户类型（1：普通；2：认证；3：达人）
        List<EnumInfo> userTypeList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.USER_TYPE, PortalUserInfoEnum.class);
        map.put("userTypeList", userTypeList);

        //注册方式（1：前台；2：后台）
        List<EnumInfo> logonModeList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.LOGON_MODE, PortalUserInfoEnum.class);
        map.put("logonModeList", logonModeList);

        return new ModelAndView("portaluser/portalUserInfo/listPage", map);
    }

    /**
     * @description 导出Excel之前查询条件校验
     * @return 返回响应对象
     */
    @RequestMapping("/isQueryData")
    @ResponseBody
    public ResponseResult isQueryData() {
        return isQueryData("portalUserInfoPd");
    }

    /**
     * description：导出列表数据到excel2007
     * user 朱正磊
     * time 2019-04-26 17:05
     * @param request 请求对象
     * @param response 响应对象
     * @param ids 选中的行数据的id拼接而成的字符串
     */
    @RequestMapping("/exportToExcel")
    public void exportToExcel(HttpServletRequest request, HttpServletResponse response, String ids) {
        log.info("【开始导出列表数据到excel2007】...");
        try {
            List<PortalUserInfoCustom> list;
            if (AssertUtil.isEmpty(ids)) {//如果没有选中行数据
                PageData pd = (PageData) getAttributeFromSession("portalUserInfoPd");
                list = portalUserInfoService.selectListAll(pd);
            } else {//如果有选中行数据
                list = portalUserInfoService.selectListByIds(ids);
            }
            Map<String, String> fieldMap = new LinkedHashMap<>();
            fieldMap.put("index", "序号");
            fieldMap.put("createTimeFormat", "创建时间");
            fieldMap.put("userId", "用户ID");
            fieldMap.put("nickName", "用户昵称");
            fieldMap.put("loginPhoneNumber", "用户手机号");
            fieldMap.put("money", "用户余额");
            fieldMap.put("userTypeDesc", "用户类型");
            fieldMap.put("userStatusDesc", "用户状态");
            fieldMap.put("logonModeDesc", "注册方式");
            fieldMap.put("followTotal", "关注数");
            fieldMap.put("fansTotal", "粉丝数");
            fieldMap.put("accessToken", "访问令牌");
            fieldMap.put("bankCardCheckPassword", "换绑银行卡验证密码");
            fieldMap.put("bankCardName", "银行卡持卡人姓名");
            fieldMap.put("bankCardNumber", "银行卡卡号");
            fieldMap.put("bankCode", "开户银行编号");
            fieldMap.put("bankName", "开户银行名称");
            fieldMap.put("bankPhoneNumber", "银行预留手机号");
            fieldMap.put("goldAlreadyProfit", "已结算收益");
            fieldMap.put("goldBuy", "充值");
            fieldMap.put("goldFreeze", "冻结");
            fieldMap.put("goldProfit", "收益");
            fieldMap.put("userIcon", "头像url");
            fieldMap.put("idNumber", "身份证号");
            fieldMap.put("isUpdateNickNameDesc", "昵称是否被修改过");
            fieldMap.put("lastLoginTimeFormat", "最近一次登录时间");
            fieldMap.put("loginPassword", "登录密码");
            fieldMap.put("publishTotal", "发布文章数");
            fieldMap.put("realName", "真实姓名");
            fieldMap.put("signature", "个性签名");
            fieldMap.put("userLevel", "用户等级");
            POIExcelUtil.exportExcelForSpecial("前端用户", request, response, list, fieldMap, true, false, false);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【exportToExcel异常信息】：{}", ExceptionUtil.logExceptionStack(e));
        }
    }

    /**
     * description：详情页面
     * user 朱正磊
     * time 2019-04-17 16:20
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/detailPage")
    public ModelAndView detailPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")) {//判断是否有查看权限
            return null;
        }

        //根据主键查询扩展类
        PortalUserInfoCustom portalUserInfo = portalUserInfoService.findCustomById(id);
        map.put("portalUserInfo", portalUserInfo);

        return new ModelAndView("portaluser/portalUserInfo/detailPage", map);
    }

    /**
     * description：添加页面
     * user 朱正磊
     * time 2019-04-17 16:26
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addPage")
    public ModelAndView addPage(Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {//判断是否有添加权限
            return null;
        }

        //用户类型（1：普通；2：认证；3：达人）
        List<EnumInfo> userTypeList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.USER_TYPE, PortalUserInfoEnum.class);
        map.put("userTypeList", userTypeList);

        //用户状态（1：启用；2：禁用；3：已删除）
        List<EnumInfo> userStatusList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.USER_STATUS, PortalUserInfoEnum.class);
        userStatusList.remove(2);
        map.put("userStatusList", userStatusList);

        //用户ID
        String userId = UUID.randomUUID().toString();
        map.put("userId", userId);

        //银行名称和银行编号
        List<BankDict> bankDictList = bankDictService.findAll();
        map.put("bankDictList", bankDictList);

        //获取省列表
        List<AreaDict> provinces = areaDictService.findByParentId(86);
        map.put("provinces", provinces);

        //用户等级
        List<UserLevelDict> userLevelDictList = userLevelDictService.findAll();
        map.put("userLevelDictList", userLevelDictList);

        //添加请求地址
        map.put("actionUrl", "admin/portalUserInfo/add");

        return new ModelAndView("portaluser/portalUserInfo/editPage", map);
    }

    /**
     * description: 根据父id查询
     * author sunny
     * createDate 2019-05-15 下午 4:45
     * @param request 请求对象
     * @return 返回响应对象
     */
    @RequestMapping("/getAreaList")
    @ResponseBody
    public ResponseResult getAreaList(HttpServletRequest request) {
        String parentId = request.getParameter("parentId");
        List<AreaDict> areas = areaDictService.findByParentId(Integer.valueOf(parentId));
        return ResponseResult.buildSuccess(areas);
    }

    /**
     * description：添加
     * user 朱正磊
     * time 2019-04-17 16:39
     * @param request 请求对象
     * @param portalUserInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/add")
    public ModelAndView add(HttpServletRequest request, PortalUserInfo portalUserInfo, Map<String, Object> map) {
        Integer flag = portalUserInfoService.add(request, portalUserInfo);
        if (Objects.equals(flag, 0)) { //添加失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：修改页面
     * user 朱正磊
     * time 2019-04-17 16:48
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        PortalUserInfoCustom portalUserInfo = portalUserInfoService.findCustomById(id);
        map.put("portalUserInfo", portalUserInfo);

        //用户类型（1：普通；2：认证；3：达人）
        List<EnumInfo> userTypeList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.USER_TYPE, PortalUserInfoEnum.class);
        map.put("userTypeList", userTypeList);

        //用户状态（1：启用；2：禁用；3：已删除）
        List<EnumInfo> userStatusList = EnumUtil.getEnumInfoList(PortalUserInfoEnum.USER_STATUS, PortalUserInfoEnum.class);
        map.put("userStatusList", userStatusList);

        //用户ID
        map.put("userId", portalUserInfo.getUserId());

        //银行名称和银行编号
        List<BankDict> bankDictList = bankDictService.findAll();
        map.put("bankDictList", bankDictList);

        //获取省列表
        List<AreaDict> provinces = areaDictService.findByParentId(86);
        map.put("provinces", provinces);

        //用户等级
        List<UserLevelDict> userLevelDictList = userLevelDictService.findAll();
        map.put("userLevelDictList", userLevelDictList);

        //修改请求地址
        map.put("actionUrl", "admin/portalUserInfo/edit");

        return new ModelAndView("portaluser/portalUserInfo/editPage", map);
    }

    /**
     * description：修改
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param request 请求对象
     * @param portalUserInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest request, PortalUserInfo portalUserInfo, Map<String, Object> map) {
        Integer flag = portalUserInfoService.updateById(request, portalUserInfo);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：禁用或者启用
     * user 朱正磊
     * time 2019-04-25 21:03
     * @param id 圈员ID
     * @param userStatus 用户状态（1：启用；2：禁用；3：已删除）
     * @return 返回响应对象
     */
    @RequestMapping("/banOrNot")
    @ResponseBody
    public ResponseResult banOrNot(Integer id, Integer userStatus) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }
        PortalUserInfo portalUserInfo = portalUserInfoService.findById(id);
        if (Objects.equals(PortalUserInfoEnum.USER_STATUS_NORMAL.getValue(), userStatus)) { //禁用
            portalUserInfo.setUserStatus((Integer) PortalUserInfoEnum.USER_STATUS_FORBIDDEN.getValue());
            Integer flag = portalUserInfoService.updateById(null, portalUserInfo);
            if (flag == 0) {
                return ResponseResult.buildResult(400, "禁用失败");
            }
            return ResponseResult.buildResult(200, "禁用成功");
        } else { //启用
            portalUserInfo.setUserStatus((Integer) PortalUserInfoEnum.USER_STATUS_NORMAL.getValue());
            Integer flag = portalUserInfoService.updateById(null, portalUserInfo);
            if (flag == 0) {
                return ResponseResult.buildResult(400, "启用失败");
            }
            return ResponseResult.buildResult(200, "启用成功");
        }
    }

    /**
     * description：批量禁用
     * user 朱正磊
     * time 2019-04-24 16:17
     * @param ids 主键集合(多个id以,进行拼接)
     * @return 返回响应对象
     */
    @RequestMapping("/batchBan")
    @ResponseBody
    public ResponseResult batchBan(String ids) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }
        Integer flag = portalUserInfoService.updateBatchBan(ids);
        if (Objects.equals(flag, 0)) {
            return ResponseResult.buildResult(400, "批量禁用失败");
        } else {
            return ResponseResult.buildResult(200, "批量禁用成功");
        }
    }

    /**
     * description：批量启用
     * user 朱正磊
     * time 2019-04-24 16:17
     * @param ids 主键集合(多个id以,进行拼接)
     * @return 返回响应对象
     */
    @RequestMapping("/batchNotBan")
    @ResponseBody
    public ResponseResult batchNotBan(String ids) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }
        Integer flag = portalUserInfoService.updateBatchNotBan(ids);
        if (Objects.equals(flag, 0)) {
            return ResponseResult.buildResult(400, "批量启用失败");
        } else {
            return ResponseResult.buildResult(200, "批量启用成功");
        }
    }

    /**
     * description：验证用户昵称不能重复
     * user 朱正磊
     * time 2019-04-28 16:27
     * @param request 请求对象
     * @return 返回响应对象
     */
    @RequestMapping("/validateSameNickName")
    @ResponseBody
    public ResponseResult validateSameNickName(HttpServletRequest request) {
        String actionUrl = request.getParameter("actionUrl");
        String nickName = request.getParameter("nickName");
        nickName = nickName.trim();//去两端空格

        List<PortalUserInfo> list;
        if (Objects.equals(actionUrl, "admin/portalUserInfo/add")) { //新增
            list = portalUserInfoService.findByNickName(nickName, null);
        } else { //修改（业务其实不允许修改，代码逻辑留着也没事）
            Integer id = Integer.valueOf(request.getParameter("id"));
            list = portalUserInfoService.findByNickName(nickName, id);
        }
        int size = list.size();
        if (Objects.equals(size, 1)) {
            return ResponseResult.buildResult(400, "该用户昵称已存在！");
        }

        return ResponseResult.buildResult(200);
    }

    /**
     * description：验证登录手机号不能重复
     * user 朱正磊
     * time 2019-04-28 16:27
     * @param request 请求对象
     * @return 返回响应对象
     */
    @RequestMapping("/validateSameLoginPhoneNumber")
    @ResponseBody
    public ResponseResult validateSameLoginPhoneNumber(HttpServletRequest request) {
        String actionUrl = request.getParameter("actionUrl");
        String loginPhoneNumber = request.getParameter("loginPhoneNumber");
        loginPhoneNumber = loginPhoneNumber.trim();//去两端空格

        List<PortalUserInfo> list;
        if (Objects.equals(actionUrl, "admin/portalUserInfo/add")) { //新增
            list = portalUserInfoService.findByLoginPhoneNumber(loginPhoneNumber, null);
        } else { //修改（业务允许修改）
            Integer id = Integer.valueOf(request.getParameter("id"));
            list = portalUserInfoService.findByLoginPhoneNumber(loginPhoneNumber, id);
        }
        int size = list.size();
        if (Objects.equals(size, 1)) {
            return ResponseResult.buildResult(400, "该登录手机号已存在！");
        }

        return ResponseResult.buildResult(200);
    }

    /**
     * description：调整金币页面
     * user 朱正磊
     * time 2019-04-29 14:02
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addOrSubPage")
    public ModelAndView addOrSubPage(Integer id, Map<String, Object> map) {
        //根据主键查询扩展类数据
        PortalUserInfoCustom portalUserInfo = portalUserInfoService.findCustomById(id);
        //主键
        map.put("id", id);
        //当前余额
        map.put("money", portalUserInfo.getMoney());
        //加减金币枚举
        List<EnumInfo> addOrSubList = EnumUtil.getEnumInfoList(UserBuyRecordEnum.BUY_TYPE, UserBuyRecordEnum.class);
        map.put("addOrSubList", addOrSubList);
        return new ModelAndView("portaluser/portalUserInfo/addOrSubPage", map);
    }

    /**
     * description：调整金币
     * user 朱正磊
     * time 2019-04-29 14:02
     * @param request 请求对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addOrSub")
    @ResponseBody
    public ModelAndView addOrSub(HttpServletRequest request, Map<String, Object> map) {
        Integer flag = portalUserInfoService.updateAddOrSub(request);
        if (Objects.equals(flag, 0)) { //调整金币失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：导出Excel（这是另外一种导出Excel的方式）
     * user 朱正磊
     * time 2019-06-05 15:54
     * @param request 请求对象
     * @param response 响应对象
     */
    @RequestMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        PageData pd = (PageData) getAttributeFromSession("portalUserInfoPd");
        String fileName = "前端用户";
        ExportExcelUtil.setResponseHeader(response, fileName);
        OutputStream out;
        BufferedOutputStream bos = null;
        String key = UUID.randomUUID().toString() + "-" + System.currentTimeMillis();
        ResponseResult responseResult;
        try {
            out = response.getOutputStream();
            bos = new BufferedOutputStream(out);
            responseResult = portalUserInfoService.portalUserInfoExportMethods(key, pd, null, null, 0);
            if (Objects.equals(responseResult.getCode(), 200)) {
                Integer count = (Integer) responseResult.getData();
                String path = request.getSession().getServletContext().getRealPath("/");
                String excelPath = path + "excel/";
                File excelFile = new File(excelPath);
                if (!excelFile.exists()) {
                    //noinspection ResultOfMethodCallIgnored
                    excelFile.mkdir();
                }
                // 压缩文件
                File zip = new File(path + "excel/"  + fileName + ".zip");
                //创建excel
                Workbook book = new HSSFWorkbook();
                String file = path + "excel/" + fileName + ".xls";
                FileOutputStream fos = new FileOutputStream(file);
                Sheet sheet;
                for (Integer i = 0; i <= count; i += 10000) {
                    ResponseResult responseResult1 = portalUserInfoService.portalUserInfoExportMethods(key, null, i, i + 10000, 1);
                    if (Objects.equals(responseResult1.getCode(), 200)) {
                        sheet = book.createSheet((i + 1) + "-" + (i + 10000));
                        //noinspection unchecked
                        List<PortalUserInfoCustom> list = (List<PortalUserInfoCustom>) responseResult1.getData();
                        toExcel(i, list, sheet);
                    }
                }
                book.write(fos);
                fos.flush();
                fos.close();
                //删除
                portalUserInfoService.portalUserInfoExportMethods(key, null, null, null, 2);

                File []srcfile = new File[1];
                srcfile[0] = new File(file);

                ExportExcelUtil.zipFiles(srcfile, zip);
                FileInputStream inStream = new FileInputStream(zip);
                BufferedInputStream bif = new BufferedInputStream(inStream);
                byte[] buf = new byte[10240];
                int readLength;
                while ((readLength = bif.read(buf)) != -1) {
                    bos.write(buf, 0, readLength);
                }
                bif.close();
                List<String> fileNames = new ArrayList<>();
                fileNames.add(file);
                ExportExcelUtil.deleteFile(fileNames, path + "excel/"  + fileName + ".zip");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert bos != null;
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * description：生成excel
     * user 朱正磊
     * time 2019-06-05 16:06
     * @param index 0,10000,20000,...
     * @param list 数据列表
     * @param sheet 工作表
     */
    private void toExcel(Integer index, List<PortalUserInfoCustom> list, Sheet sheet) {
        try {
            //设置列宽
            sheet.setColumnWidth(0, ((6 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(1, ((30 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((30 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(7, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(8, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(9, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(10, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(11, ((35 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(12, ((35 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(13, ((35 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(14, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(15, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(16, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(17, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(18, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(19, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(20, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(21, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(22, ((35 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(23, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(24, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(25, ((30 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(26, ((35 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(27, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(28, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(29, ((50 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(30, ((20 + 1) * 7 + 5) / 7 * 256);

            //创建第一行
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("创建时间");
            row.createCell(2).setCellValue("用户ID");
            row.createCell(3).setCellValue("用户昵称");
            row.createCell(4).setCellValue("用户手机号");
            row.createCell(5).setCellValue("用户余额");
            row.createCell(6).setCellValue("用户类型");
            row.createCell(7).setCellValue("用户状态");
            row.createCell(8).setCellValue("注册方式");
            row.createCell(9).setCellValue("关注数");
            row.createCell(10).setCellValue("粉丝数");
            row.createCell(11).setCellValue("访问令牌");
            row.createCell(12).setCellValue("换绑银行卡验证密码");
            row.createCell(13).setCellValue("银行卡持卡人姓名");
            row.createCell(14).setCellValue("银行卡卡号");
            row.createCell(15).setCellValue("开户银行编号");
            row.createCell(16).setCellValue("开户银行名称");
            row.createCell(17).setCellValue("银行预留手机号");
            row.createCell(18).setCellValue("已结算收益");
            row.createCell(19).setCellValue("充值");
            row.createCell(20).setCellValue("冻结");
            row.createCell(21).setCellValue("收益");
            row.createCell(22).setCellValue("头像url");
            row.createCell(23).setCellValue("身份证号");
            row.createCell(24).setCellValue("昵称是否被修改过");
            row.createCell(25).setCellValue("最近一次登录时间");
            row.createCell(26).setCellValue("登录密码");
            row.createCell(27).setCellValue("发布文章数");
            row.createCell(28).setCellValue("真实姓名");
            row.createCell(29).setCellValue("个性签名");
            row.createCell(30).setCellValue("用户等级");

            for (int i = 0; i < list.size(); i++) {
                PortalUserInfoCustom custom = list.get(i);
                Row dateRow = sheet.createRow(i + 1);
                dateRow.createCell(0).setCellValue(index + i + 1);
                for (int cellNum = 1; cellNum <= 30; cellNum++) {
                    Cell cell = dateRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            cell.setCellValue(custom.getCreateTimeFormat());
                            break;
                        }
                        case 2: {
                            cell.setCellValue(custom.getUserId());
                            break;
                        }
                        case 3: {
                            cell.setCellValue(custom.getNickName());
                            break;
                        }
                        case 4: {
                            cell.setCellValue(custom.getLoginPhoneNumber());
                            break;
                        }
                        case 5: {
                            cell.setCellValue(custom.getMoney());
                            break;
                        }
                        case 6: {
                            cell.setCellValue(custom.getUserTypeDesc());
                            break;
                        }
                        case 7: {
                            cell.setCellValue(custom.getUserStatusDesc());
                            break;
                        }
                        case 8: {
                            cell.setCellValue(custom.getLogonModeDesc());
                            break;
                        }
                        case 9: {
                            cell.setCellValue(custom.getFollowTotal());
                            break;
                        }
                        case 10: {
                            cell.setCellValue(custom.getFansTotal());
                            break;
                        }
                        case 11: {
                            cell.setCellValue(custom.getAccessToken());
                            break;
                        }
                        case 12: {
                            cell.setCellValue(custom.getBankCardCheckPassword());
                            break;
                        }
                        case 13: {
                            cell.setCellValue(custom.getBankCardName());
                            break;
                        }
                        case 14: {
                            cell.setCellValue(custom.getBankCardNumber());
                            break;
                        }
                        case 15: {
                            cell.setCellValue(custom.getBankCode());
                            break;
                        }
                        case 16: {
                            cell.setCellValue(custom.getBankName());
                            break;
                        }
                        case 17: {
                            cell.setCellValue(custom.getBankPhoneNumber());
                            break;
                        }
                        case 18: {
                            cell.setCellValue(custom.getGoldAlreadyProfit());
                            break;
                        }
                        case 19: {
                            cell.setCellValue(custom.getGoldBuy());
                            break;
                        }
                        case 20: {
                            cell.setCellValue(custom.getGoldFreeze());
                            break;
                        }
                        case 21: {
                            cell.setCellValue(custom.getGoldProfit());
                            break;
                        }
                        case 22: {
                            cell.setCellValue(custom.getUserIcon());
                            break;
                        }
                        case 23: {
                            cell.setCellValue(custom.getIdNumber());
                            break;
                        }
                        case 24: {
                            cell.setCellValue(custom.getIsUpdateNickNameDesc());
                            break;
                        }
                        case 25: {
                            cell.setCellValue(custom.getLastLoginTimeFormat());
                            break;
                        }
                        case 26: {
                            cell.setCellValue(custom.getLoginPassword());
                            break;
                        }
                        case 27: {
                            cell.setCellValue(custom.getPublishTotal());
                            break;
                        }
                        case 28: {
                            cell.setCellValue(custom.getRealName());
                            break;
                        }
                        case 29: {
                            cell.setCellValue(custom.getSignature());
                            break;
                        }
                        case 30: {
                            cell.setCellValue(custom.getUserLevel());
                            break;
                        }
                        default:
                            break;
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
